<template>
    <div id="app">
        <fieldset>
            <legend>{{isLogin?"内容界面":"登入界面"}}</legend>
            <!-- 内容展示界面 -->
            <div class="content" v-if="isLogin">
                <h1>列表展示</h1>
                <button @click="isLogin=false">点击退出登入</button>
                <button @click="getList">点击获取列表数据</button>
                <h2>
                    <img :src="userInfo.headimgurl" width="50" alt="">
                    昵称:
                    <a href="">{{userInfo.nickname}}</a>
                </h2>
                <ul>
                    <li v-for="item in lists" :key="item._id">
                        <b>{{item.name}}</b>
                        <button v-has:read="permissionBtns">查看</button>
                        <button v-has:edit="permissionBtns">修改</button>
                        <button v-has:delete="permissionBtns">删除</button>
                        <button v-has:add="permissionBtns">增加</button>
                    </li>
                </ul>
            </div>
            <!-- 登入界面 -->
            <div class="login" v-else>
                <h1>请登入</h1>
                <label for="username">用户名:</label>
                <input type="text" id="username" v-model="username">
                <br>
                <label for="password">密码:</label>
                <input type="text" v-model="password" @keydown.enter="login">
                <br>
                <button @click="login">登入</button>
            </div>
        </fieldset>
    </div>
</template>

<script>
const loginApi = (params = {}) => {
        return fetch('http://chst.vip/users/login', {
            //请求方式
            method: 'post',
            //请求头
            headers: {
                'content-type': 'application/json'
            },
            //携带的参数(上载的参数)
            body: JSON.stringify(params)
        })
            .then(body => body.json())
            .catch(err => err.toString())
    }

    //获取列表的请求

    const getListApi = () => {
        return fetch('http://chst.vip/students/getstulist?count=1', {
            method: 'get',
            headers: {
                'authorization': sessionStorage.getItem('user-token')
            }
        })

            .then(body => body.json())
            .catch(err => err.toString())
    }

    //声明全局的自定义指令,对按钮进行鉴权操作
export default{
        methods: {
            //登入
            async login() {
                //使用async和await来等待请求的异步结果
                let res = await loginApi({ username: this.username, password: this.password });
                console.log(res)
                if (res.state) {
                    //登入成功
                    this.isLogin = true;
                    //设置用户信息
                    this.userInfo = res.userInfo;
                    //将用户的令牌存到本地
                    sessionStorage.setItem('user-token', res.token)
                    //给permissionBtns赋值
                    this.permissionBtns = res.permission.buttons;
                    //直接获取列表
                    this.getList()
                } else {
                    alert(res.msg)
                }
            },
            
        },
        directives: {
                'has':{inserted(el, binding) {
            console.log(binding)
            //按钮需要的权限edit,从指令参数湖区
            let role = binding.arg;
            //用户权限 [read,add] 从表达式里面获取
            let userRole = binding.value;
            //判断用户有没有按钮的权限
            let hasRole = userRole.includes(role);
            if (!hasRole) {
                //没有权限,那么就禁用当前的按钮
                //1.禁用
                // el.disabled = true;
                //2.直接不显示,移除
                el.parentNode.removeChild(el)
            }}
            },
            //获取列表
            async getList() {
                let res = await getListApi()
                //判断是否请求成功
                if (res.state) {
                    //请求成功
                    this.lists = res.data
                } else {
                    alert(res.msg)
                }
            }
        },
        data () {
            return {
                //是否登入的状态
            isLogin: false,
            username: "",
            password: "",
            //ueserInfo 用户信息
            userInfo: {},
            lists: [],
            //权限按钮
            permissionBtns: []
            }
        }
    }
</script>

<style>
 ul {
        padding: 0;
        margin: 0;
        list-style: none;
    }

    li {
        /* border: 1px solid deeppink; */
        margin: 10px;
        max-width: 500px;
    }
</style>